package com.lemon.smartsafety.ui.activity.user;

import android.os.CountDownTimer;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.OldBaseMode;
import com.lemon.smartsafety.result.user.EmailVerCodeMode;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.RulesUtils;
import com.lemon.smartsafety.utils.SystemUtil;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.Bind;
import butterknife.OnClick;

public class BindEmailForLoginActivity extends NewBaseActivity {


    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.txt_ok)
    TextView txtOk;
    @Bind(R.id.metRemarkEmail)
    MaterialEditText metRemarkEmail;
    @Bind(R.id.metRemarkVerCode)
    MaterialEditText metRemarkVerCode;
    @Bind(R.id.txt_verCode)
    TextView txtVerCode;


    private int verCode=0;
    private boolean isShowGetCode=false;
    private EmailVerCodeMode emailVerCodeMode;

    @Override
    protected void getBundleArgs() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_bind_email_for_login;
    }

    @Override
    protected void initTitleView() {
        setTitle("绑定邮箱");
    }

    @Override
    protected void initView() {
        metRemarkEmail.setText("");

    }

    @Override
    protected void initHttp() {

    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }


    @OnClick({R.id.txt_ok,R.id.txt_verCode})
    public void onSubmitClick(View view) {
        switch (view.getId()) {
            case R.id.txt_verCode:
                SystemUtil.HideKeyboard(mActivity);
                if(verCode<=0&&doVerCode()){
                    httpPostEmailVerCode();
                    starVarCode();
                }
                break;
            case R.id.txt_ok:
                SystemUtil.HideKeyboard(mActivity);
                if (doEmail()) {
                    httpResetEmail();
                }
                break;
        }
    }


    /**
     * 邮箱验证码
     *
     * @return
     */
    private boolean doVerCode() {

        if (CommonUtil.strIsEmpty(metRemarkEmail.getText().toString().trim())) {
            mHUDUtil.showDoneTip("请输入邮箱");
            return false;
        }

        if (!RulesUtils.isEmail(metRemarkEmail.getText().toString().trim())) {
            mHUDUtil.showDoneTip("邮箱不合法");
            return false;
        }


        return true;
    }



    /**
     * 邮箱验证
     *
     * @return
     */
    private boolean doEmail() {

        if (CommonUtil.strIsEmpty(metRemarkEmail.getText().toString().trim())) {
            mHUDUtil.showDoneTip("请输入邮箱");
            return false;
        }

        if (!RulesUtils.isEmail(metRemarkEmail.getText().toString().trim())) {
            mHUDUtil.showDoneTip("邮箱不合法");
            return false;
        }

        if (CommonUtil.strIsEmpty(metRemarkVerCode.getText().toString().trim())) {
            mHUDUtil.showDoneTip("请输入邮箱验证码");
            return false;
        }

        return true;
    }

    /**
     *  启动读秒倒计时
     */
    private void starVarCode(){
        SystemUtil.HideKeyboard(mActivity);
        verCode=60;
        isShowGetCode=true;

        new CountDownTimer(60000, 1000) {

            public void onTick(long millisUntilFinished) {

                if(isShowGetCode) {
                    verCode--;
                    txtVerCode.setText(verCode + "s");
                }else{
                    txtVerCode.setText("重新获取");
                    verCode=-1;
                }
            }

            public void onFinish() {
                if(isShowGetCode) {
                    txtVerCode.setText("重新获取");
                    isShowGetCode=false;
                    verCode=-1;
                }
            }

        }.start();


    }





    /**
     *  发送邮箱验证码
     */
    private void httpPostEmailVerCode() {
        emailVerCodeMode=null;

        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("login_name", mSharedPreferencesHelper.getBindLoginName());
        params.put("email", metRemarkEmail.getText().toString().trim());
        params.put("type", "email");

        showLoading("");
        SharedPreferencesServer mSharedPreferencesServer = new SharedPreferencesServer(this);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(HttpPath.Bind_email);

        HttpOkBase.httpBasePost(params, serverUrl, "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                dismissLoading();
                if (httpClient) {
                    OldBaseMode oldBaseMode = JsonUtils.fromJson(result, OldBaseMode.class);
                    if (!CommonUtil.objIsEmpty(oldBaseMode)) {

                        if (!oldBaseMode.isIsError()) {
                            emailVerCodeMode = JsonUtils.fromJson(mGson.toJson(oldBaseMode.getResult()), EmailVerCodeMode.class);
                        }else{
                            verCode=-1;
                            isShowGetCode=false;
                        }
                        ToastUtil.show(oldBaseMode.getError().getMsg());
                    }
                }
            }
        });


    }






    /**
     * 绑定邮箱
     */
    private void httpResetEmail() {

        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("login_name", mSharedPreferencesHelper.getBindLoginName());
        params.put("email", metRemarkEmail.getText().toString().trim());
        params.put("verify_code", metRemarkVerCode.getText().toString().trim());

        showLoading("");

        SharedPreferencesServer mSharedPreferencesServer = new SharedPreferencesServer(this);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(HttpPath.Bind_email_check);

        HttpOkBase.httpBasePost(params, serverUrl, "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                dismissLoading();
                if (httpClient) {
                    OldBaseMode oldBaseMode = JsonUtils.fromJson(result, OldBaseMode.class);
                    if (!CommonUtil.objIsEmpty(oldBaseMode)) {

                        if (!oldBaseMode.isIsError()) {
                            mSharedPreferencesHelper.setEmail(metRemarkEmail.getText().toString().trim());
                            ToastUtil.show(oldBaseMode.getError().getMsg());

                            finish();
                        } else {
                            ToastUtil.show(oldBaseMode.getError().getMsg());
                        }
                    }
                }
            }
        });

    }


}
